<?php

//decode by 012wz.com QQ:800083075
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$openid = m('user')->getOpenid();
$member = m('member')->getMember($openid, true);
$shop = m('common')->getSysset('shop');
$uniacid = $_W['uniacid'];
if ($_W['isajax']) {
	if ($operation == 'display') {
		$pindex = max(1, intval($_GPC['page']));
		$psize = 10;
		$condition = ' and log.openid=:openid and log.status>0 and  log.uniacid = :uniacid';
		$params = array(':uniacid' => $_W['uniacid'], ':openid' => $openid);
		$sql = 'SELECT COUNT(*) FROM ' . tablename('ewei_shop_goldshop_log') . " log where 1 {$condition}";
		$total = pdo_fetchcolumn($sql, $params);
		$list = array();
		if (!empty($total)) {
			$sql = 'SELECT log.id,log.goodsid,log.status,log.eno,log.paystatus,g.title,g.type,g.thumb,g.credit,g.money,log.credit,log.weixin,log.gold_card,log.dispatchstatus FROM ' . tablename('ewei_shop_goldshop_log') . ' log ' . ' left join ' . tablename('ewei_shop_goldshop_goods') . ' g on log.goodsid = g.id ' . ' where 1 ' . $condition . ' ORDER BY log.createtime DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
			$list = pdo_fetchall($sql, $params);
			$list = set_medias($list, 'thumb');
			foreach ($list as &$row) {
				if ($row['credit'] > 0 & $row['money'] > 0) {
					$row['acttype'] = 0;
				} else {
					if ($row['credit'] > 0) {
						$row['acttype'] = 1;
					} else {
						if ($row['money'] > 0) {
							$row['acttype'] = 2;
						}
					}
				}
			}
			unset($row);
		}
		show_json(1, array('total' => $total, 'list' => $list, 'pagesize' => $psize));
	} else {
		if ($operation == 'detail') {
			$id = intval($_GPC['id']);
			$log = pdo_fetch('select * from ' . tablename('ewei_shop_goldshop_log') . ' where id=:id and openid=:openid and uniacid=:uniacid limit 1', array(':id' => $id, ':openid' => $openid, ':uniacid' => $uniacid));
			if (empty($log)) {
				show_json(-1, '兑换记录不存在!');
			}
			$goods = $this->model->getGoods($log['goodsid'], $member);
			if (empty($goods['id'])) {
				show_json(-1, '商品记录不存在!');
			}
			$address = false;
			if (!empty($log['addressid'])) {
				$address = pdo_fetch('select id,realname,mobile,address,province,city,area from ' . tablename('ewei_shop_member_address') . ' where id=:id and openid=:openid and uniacid=:uniacid limit 1', array(':id' => $log['addressid'], ':uniacid' => $uniacid, ':openid' => $openid));
			}
			$goods['currenttime'] = time();
                        //已完成
                        if(!empty($_GPC['status']) && !empty($_GPC['id'])){
                        pdo_update('ewei_shop_goldshop_log', array('status' =>$_GPC['status'], 'finishtime' =>time()), array('id' =>$_GPC['id']));
                        $info = ' 收件人: ' . $log['realname'] . ' 联系电话: ' . $log['mobile'] . ' 收货地址: ' . $log['province'] . $log['city'] . $log['area'] . ' ' . $log['address'];
                          $msg = array(
                              'first' => array('value' => "金币商城兑换确认收货通知！！~", "color" => "#4a5077"),
                              'keyword1' => array('title' => '订单号', 'value' =>$log['expresssn'] , "color" => "#4a5077"),
                              'keyword2' => array('title' => '商品名称', 'value' =>$goods['title'], "color" => "#4a5077"), 
                              'keyword3' => array('title' => '下单时间', 'value' =>date('Y-m-d H:i:s',$log['createtime']), "color" => "#4a5077"),
                              'keyword4' => array('title' => '发货时间', 'value' =>date('Y-m-d H:i:s',$log['sendtime']), "color" => "#4a5077"),
                              'keyword5' => array('title' => '确认发货时间', 'value' =>date('Y-m-d H:i:s',time()), "color" => "#4a5077"),
                              'keyword6' => array('title' => '收货信息', 'value' =>$info, "color" => "#4a5077")
                          );
                        m('message')->sendCustomNotice($goods['noticeopenid'], $msg);
                        }
			show_json(1, array('log' => $log, 'goods' => $goods, 'address' => $address));
		} else {
			if ($operation == 'paydispatch' && $_W['ispost']) {
				$id = intval($_GPC['id']);
				$addressid = intval($_GPC['addressid']);
				$log = pdo_fetch('select * from ' . tablename('ewei_shop_goldshop_log') . ' where id=:id and openid=:openid and uniacid=:uniacid limit 1', array(':id' => $id, ':openid' => $openid, ':uniacid' => $uniacid));
				if (empty($log)) {
					show_json(0, '兑换记录不存在!');
				}
				$goods = $this->model->getGoods($log['goodsid'], $member);
				if (empty($goods['id'])) {
					show_json(0, '商品记录不存在!');
				}
				if (!empty($goods['isendtime'])) {
					if (time() > $goods['endtime']) {
						show_json(0, '商品已过期!');
					}
				}
				if ($goods['dispatch'] < 0) {
					show_json(0, '商品不需要支付运费!');
				}
				if (!empty($log['dispatchstatus'])) {
					show_json(0, '商品已支付运费!');
				}
				$set = m('common')->getSysset();
				if (!is_weixin()) {
					show_json(0, '非微信环境!');
				}
				if (empty($set['pay']['weixin'])) {
					show_json(0, '未开启微信支付!');
				}
				$wechat = array('success' => false);
				$dispatchno = $log['dispatchno'];
				if (empty($dispatchno)) {
					if (empty($goods['type'])) {
						$dispatchno = str_replace("EE", "EP", $log['logno']);
					} else {
						$dispatchno = str_replace("EL", "EP", $log['logno']);
					}
					pdo_update('ewei_shop_goldshop_log', array('dispatchno' => $dispatchno, 'addressid' => $addressid), array('id' => $log['id']));
				}
				$params = array();
				$params['tid'] = $dispatchno;
				$params['user'] = $openid;
				$params['fee'] = $goods['dispatch'];
				$params['title'] = $set['shop']['name'] . (empty($goods['type']) ? "金币兑换" : '积分抽奖') . ' 支付运费单号:' . $dispatchno;
				load()->model('payment');
				$setting = uni_setting($_W['uniacid'], array('payment'));
				if (is_array($setting['payment'])) {
					$options = $setting['payment']['wechat'];
					$options['appid'] = $_W['account']['key'];
					$options['secret'] = $_W['account']['secret'];
					$wechat = m('common')->wechat_build($params, $options, 3);
					$wechat['success'] = false;
					if (!is_error($wechat)) {
						$wechat['success'] = true;
					} else {
						show_json(0, $wechat['message']);
					}
				}
				if (!$wechat['success']) {
					show_json(0, '微信支付参数错误!');
				}
				show_json(1, array('logid' => $logid, 'wechat' => $wechat));
			} else {
				if ($operation == 'payresult' && $_W['ispost']) {
					$id = intval($_GPC['id']);
					$log = pdo_fetch('select * from ' . tablename('ewei_shop_goldshop_log') . ' where id=:id and openid=:openid and uniacid=:uniacid limit 1', array(':id' => $id, ':openid' => $openid, ':uniacid' => $uniacid));
					if (empty($log)) {
						show_json(0, '兑换记录不存在!');
					}
					$goods = $this->model->getGoods($log['goodsid'], $member);
					if (empty($goods['id'])) {
						show_json(0, '商品记录不存在!');
					}
					if ($goods['dispatch'] > 0 && empty($log['dispatchstatus'])) {
						pdo_update('ewei_shop_goldshop_log', array('dispatchstatus' => 1), array('id' => $log['id']));
					}
					$this->model->sendMessage($id);
					show_json(1);
				}
			}
		}
	}
}
$_W['shopshare'] = array('title' => $this->set['share_title'], 'imgUrl' => tomedia($this->set['share_icon']), 'link' => $this->createPluginMobileUrl('goldshop'), 'desc' => $this->set['share_desc']);
$com = p('commission');
if ($com) {
	$cset = $com->getSet();
	if (!empty($cset)) {
		if ($member['isagent'] == 1 && $member['status'] == 1) {
			$_W['shopshare']['link'] = $this->createPluginMobileUrl('goldshop', array('mid' => $member['id']));
			if (empty($cset['become_reg']) && (empty($member['realname']) || empty($member['mobile']))) {
				$trigger = true;
			}
		} else {
			if (!empty($_GPC['mid'])) {
				$_W['shopshare']['link'] = $this->createPluginMobileUrl('goldshop/detail', array('mid' => $_GPC['mid']));
			}
		}
	}
}
if ($operation == 'display') {
	include $this->template('log');
} elseif ($operation == 'detail') {
	include $this->template('log_detail');
}